<?php
/**
 * @desc 行程管理数据层
 * @author kevin.wang
 * @date 2015-01-01 12:00:00
 */
class Trip extends Base
{
	//数据表
	protected $table = 'pert_daily_trip';
	//首页焦点广告
	public function getTripList(array $con)  
    {
		 $table = $this->table;
		 $filed = "`daily_trip_id`,`trip_name`,`goods_id`,`hotal`,`hot_pro`,`hotal_start`,`food`,`detail`,`hotal_image`,`trip_image`,`guide`,`feature`,`traffic` ,`spot`,`trip_pro`,`buy_tips`,`delete_flag`,`raw_add_time`,`raw_update_time`,`sort`,`author`,`is_show`";
		 $sql = "select {$filed} from `{$table}` where `goods_id` = :goods_id and `delete_flag` = 0";
		 $where = "";
		 if(!empty($con['trip_name'])){
			 $where .= " and `trip_name` like '%{$con['trip_name']}%'";
		 }
		 $limit = " order by `sort` desc, `daily_trip_id` desc limit ".(($con['current'] - 1)*$con['numPerPage']).", ".$con['numPerPage'];
		 $return = $this->select_table($sql.$where.$limit, array(':goods_id'=>$con['goods_id']));
		 $sql = "select count(`daily_trip_id`) as num from `{$table}` where `goods_id` = :goods_id and `delete_flag` = 0";
		 $return[] = $this->select_table($sql.$where, array(':goods_id'=>$con['goods_id']), Base::QUERY_COLUMN);
		 return $return;
    }
	//删除行程
	public function deleteTrip($ids){
		$table = $this->table;
		$sql = "update `{$table}` set `delete_flag` = 1 where `daily_trip_id` in ({$ids})";
		return $this->update_table($sql, array());
	}
	//写入行程
	public function insertTrip(array $data){
		if(empty($data)){
			return false;
		}
		return self::insert_table($data, $this->table);
	}
	//获取行程
	public function getTrip($id){
		$table = $this->table;
		$filed = "`daily_trip_id`,`trip_name`,`goods_id`,`hotal`,`hot_pro`,`hotal_start`,`food`,`detail`,`hotal_image`,`trip_image`,`guide`,`feature`,`traffic` ,`spot`,`trip_pro`,`buy_tips`,`delete_flag`,`raw_add_time`,`raw_update_time`,`sort`,`author`,`is_show`";
		$sql = "select {$filed} from `{$table}` where `daily_trip_id` = :daily_trip_id and `delete_flag` = 0";
		return $this->select_table($sql, array(':daily_trip_id' => $id), Base::QUERY_RAW);
	}
	//修改行程
	public function editTrip($id, array $data){
		if(empty($id) || empty($data)){
			return false;
		}
		$table = $this->table;
		$sql = "update `{$table}` set ";
		$binds = array();
		foreach($data as $key=>$value){
			$sql .= "`{$key}` = :{$key},";
			$binds[":{$key}"] = $value;
		}
		$sql = trim($sql, ',');
		$sql .= " where `daily_trip_id` = :daily_trip_id";
		$binds[':daily_trip_id'] = $id;
		return $this->update_table($sql, $binds);
	}
}
